package com.elitel.frame.base.service.impl;

import com.elitel.frame.main.entity.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.elitel.frame.core.service.impl.BaseServiceImpl;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;

import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.component.RedisCacheDataBase;
import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.main.dao.CfgDbMapper;
import com.elitel.frame.main.dao.ext.ExtCfgDbMapper;
import com.elitel.frame.main.entity.CfgDb;
import org.springframework.transaction.annotation.Transactional;

import static com.elitel.frame.main.entity.DataSourceType.DM;

/**
 * @Description 数据库链接配置服务实现类
 * @author guoyanfei
 */
@Service
public class CfgDbServiceImpl extends BaseServiceImpl
		implements CfgDbService{

	@Autowired
	private ExtCfgDbMapper extCfgDbMapper;
	@Autowired
	private CfgDbMapper cfgDbMapper;
	@Autowired
	private RedisCacheComponent redisCacheComponent;
	@Autowired
	private RedisCacheDataBase redisCacheDataBase;
	@Autowired
	private DataBaseConnInfo dataBaseConnInfo;

	@Override
	@Transactional
	public PageResponseVo<CfgDb> getListByPage(PageRequestVo<CfgDb> pr) {
		String databaseType = dataBaseConnInfo.dataBaseType;
		List<CfgDb> list = new ArrayList<CfgDb>();
		switch (databaseType) {
			case "oracle":
			case DM:
				pr.StartPage();
				list = extCfgDbMapper.getAllList(pr);
				break;
			case "mysql":
			case "sqlite":
			case "h2":
				pr.setPageNo((pr.getPageNo() - 1) * pr.getPageSize());
				list = extCfgDbMapper.getAllListMysql(pr);
				break;
			case DataSourceType.POSTGRES:
				pr.setPageNo((pr.getPageNo() - 1) * pr.getPageSize());
				list = extCfgDbMapper.getAllListPG(pr);
				break;
		}

		Integer total = extCfgDbMapper.getListCount(pr);
		PageResponseVo<CfgDb> responseVo = new PageResponseVo<CfgDb>();
		responseVo.setRows(list);
		responseVo.setTotal(total);

		return responseVo;
	}

	@Override
	@Transactional
	public int insertData(CfgDb cfgDb) {
		cfgDb.setDbSign(UUID.randomUUID().toString());
//		cfgDb.getCreatedBy();
		cfgDb.setCreatedTime(new java.util.Date());
		int insertSelective = cfgDbMapper.insertSelective(cfgDb);
		redisCacheComponent.freshCfgDBRedisCache();
		redisCacheComponent.freshTreeReportData();
		redisCacheDataBase.refreshTreelistRedisCache();
		redisCacheDataBase.refreshTreeViewRedisCache();
		return insertSelective;
	}

	@Override
	@Transactional
	public int updateData(CfgDb cfgDb) {
		cfgDb.setDbSign(UUID.randomUUID().toString());
		int updateByPrimaryKeySelective = cfgDbMapper.updateByPrimaryKeySelective(cfgDb);
		//刷新redis缓存
		redisCacheComponent.freshCfgDBRedisCache();
		redisCacheComponent.freshTreeReportData();
		redisCacheDataBase.refreshTreelistRedisCache();
		redisCacheDataBase.refreshTreeViewRedisCache();

		return updateByPrimaryKeySelective;
	}

	@Override
	@Transactional
	public void deleteBatchByKey(List<String> keys) {
		extCfgDbMapper.deleteBatchByKey(keys);
		redisCacheComponent.freshCfgDBRedisCache();
		redisCacheComponent.freshTreeReportData();
		redisCacheDataBase.refreshTreelistRedisCache();
		redisCacheDataBase.refreshTreeViewRedisCache();
	}

	@Override
	public CfgDb selectByKey(String key) {
		return cfgDbMapper.selectByPrimaryKey(key);
	}

	@Override
	@Transactional
	public CfgDb queryByKey(String dbSign) {
		return extCfgDbMapper.selectByPrimaryDBSign(dbSign);
	}

	@Override
	@Transactional
	public CfgDb getDbInfoBydtconfigID(String dtConfigId) {
		return extCfgDbMapper.getDbInfoBydtconfigID(dtConfigId);
	}

	@Override
	@Transactional
	public List<CfgDb> getCfgDBList() {
		return extCfgDbMapper.getAllListCfgDb();
	}

	@Override
	@Transactional
	public List<CfgDb> getAllListCfgDb() {
		return extCfgDbMapper.getAllListCfgDb();
	}

	@Override
	public List<CfgDb> getDbListByDbType(String dbType) {
		return extCfgDbMapper.getDbListByDbType(dbType);
	}

    @Override
    public int getCount() {
        return cfgDbMapper.getCount();
    }

    @Override
    public List<CfgDb> dataSourceStatus()
	{
		return extCfgDbMapper.dataSourceStatus();
    }
}
